package com.hejr.test02.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hejr.entity.User;
import com.hejr.test01.service.IUserOneService;
import com.hejr.test02.mapper.UserTwoMapper;
import com.hejr.test02.service.IUserTwoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: hejr
 * @desc: 第二个用户库表业务层
 * @date: 2019/4/9 11:02
 */
@Service("userTwoService")
@Slf4j
public class UserTwoServiceImpl extends ServiceImpl<UserTwoMapper, User> implements IUserTwoService {


    @Autowired
    private IUserOneService userOneService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer addUser(String name, Integer age) {
        log.info("第二库新增用户，参数：[name：{}，age：{}]", name, age);
        User user = User.builder().name(name).age(age).build();
        int ret = baseMapper.insert(user);
        int result = 10 / age;
        return ret;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer addAllUser(String name, Integer age) {
        log.info("两库同时新增用户，参数：[name：{}，age：{}]", name, age);
        //第一个数据源
        Integer oneRet = userOneService.addUser(name, age);
        //第二个数据源
        User user = User.builder().name(name).age(age).build();
        int twoRet = baseMapper.insert(user);
        int ret = 10 / age;
        return oneRet + twoRet;
    }
}
